<link href="<?php echo URL::base() ?>media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.10.2.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-1.10.4.custom.js"></script>
<script src="<?php echo URL::base() ?>media/laydate/laydate.js"></script>
<style type="text/css">
    .display_div {
        display: none
        }
    .uppercase {
        text-transform: uppercase;
        }
    .table thead tr th {
        text-align: center;
        }

    .table tbody tr th {
        font-weight: normal;
        color: #333333;
        text-align: center;
        border: 1px solid #fff;
        }

    .table tfoot tr td {
        border: 1px solid #ffffff;
        text-align: center;
        }

    .table tfoot tr {
        background-color: #eeeeee;
        }

    table tbody tr:nth-child(2n) {
        background-color: #f5f5f5;
        }
</style>
<!-- 查询 -->
<form class="form-horizontal" action="<?php echo URL::site() ?>qualityfrom/detail/select" method="get" id="myfrom">
    <div class="control-group">
        <label class="control-label">质检时间:</label>
        <div class="controls">
            <input readonly="readonly" class="input-medium datepicker" type="text"
                   value="<?php if (isset($_GET['invoice_date_from'])) {
                       echo $_GET['invoice_date_from'];
                   } ?>" name='invoice_date_from'/ id="invoice_date_from"/>
            到 <input readonly="readonly" class="input-medium datepicker" type="text"
                     value="<?php if (isset($_GET['invoice_date_to'])) {
                         echo $_GET['invoice_date_to'];
                     } ?>" name='invoice_date_to' id="invoice_date_to"/>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label">SKU:</label>
        <div class="controls">
            <input type="text" class="uppercase" name="sku" id="sku" value="<?php echo $_GET['sku'];?>">
        </div>
    </div>
    <div class="control-group">
        <label class="control-label">供应商:</label>
        <div class="controls">
            <input type="text"  class="input-xlarge" name="supplier_name" id="supplier_name" value="<?php echo $_GET['supplier_name'];?>" >
            <input type="hidden"   name="supplier_id" id="supplier_id" value="<?php echo $_GET['supplier_id'];?>" >
        </div>
    </div>
    <div class="control-group">
        <label class="control-label">主体:</label>
        <div class="controls">
            <select name="enterprise_dominant" class="input-xlarge" id="enterprise_dominant">
                <option value="">选择主体</option>
                <?php foreach ($enterprise_dominant as $key =>$val){?>
                    <option value="<?php echo $key?>" <?php echo $_GET['enterprise_dominant']==$key?'selected="selected"':'';?>><?php echo $val?></option>
                <?php }?>
            </select>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label">是否出口退税:</label>
        <div class="controls">
            <select name="export_tax_rebate" id="export_tax_rebate" class="input2">
                <option value="-1">请选择</option>
                <option value="1" <?php if(isset($_GET['export_tax_rebate']) && 1==$_GET['export_tax_rebate'])echo 'selected';?>>是</option>
                <option value="0" <?php if(isset($_GET['export_tax_rebate']) && 0==$_GET['export_tax_rebate'])echo 'selected';?>>否</option>
            </select>
        </div>
    </div>

    <div class="control-group">
        <div class="controls">
<!--        <input type="hidden" name="per_page" value="50" id="per_page">-->
<!--        <input type="hidden" name="current_page" value="0" id="current_page">-->
        <button type="submit" id="search_btn_not" class="btn btn-info">查询</button>
        <button type="submit" id="search_load" name="down" value="1" class="btn">导出</button>
            <button type="reset" class="btn" onclick="resetDate()">重置</button>
        </div>
    </div>
</form>


<!-- 查询结果 -->

<table class="table table-bordered" id="table_content" style="border-collapse:collapse;">
<thead>
<tr style=" background-color:#4EA392;color:#fff;">
<th style="width:30px;">序号</th>
<th style="width:85px;">质检日期</th>
<th style="width:90px;">收货明细单号</th>
<th style="width:90px;">VMI预约单号</th>
<th style="width:90px;">是否出口退税</th>
<th>SKU</th>
<th>中文名称</th>
    <th>仓库</th>
    <th>主体</th>
<th style="min-width:30px;">规格</th>
<th>储位</th>
<th>供应商</th>
<th style="width:60px;">采购数</th>
<th style="width:60px;">质检数</th>
<th style="width:60px;">合格数</th>
<th style="width:75px;">不合格数</th>
<th style="width:50px;">良品率</th>
</tr>
</thead>
<tbody id="div_content">

<?php
if(isset($quality_lists) and $quality_lists){
    foreach ($quality_lists as $num => $quality_list){?>
        <tr>
            <th><?php echo $num+1+$current_page ?></th>
            <th><?php echo $quality_list['invoice_date'] ?></th>
            <th><?php echo $quality_list['recieve_detail_id'] ?></th>
            <th><?php echo $quality_list['purchaseorder_id'] ?></th>
            <th><?php if($quality_list['export_tax_rebate']==0){
                    echo '非出口退税';
                }else{
                    echo '出口退税';
                } ;?></th>
            <th><?php echo $quality_list['sku'] ?></th>
            <th><?php echo $quality_list['sku_name'] ?></th>
            <th><?php echo $vmi_warehouse[$quality_list['warehouseid']]; ?></th>
            <th><?php echo $enterprise_dominant[$quality_list['enterprise_dominant']]; ?></th>
            <th><?php echo $quality_list['sku_standard'] ?></th>
            <th><?php echo $quality_list['storage_position'] ?></th>
            <th><?php echo Basic::getnameByid($quality_list['supplier_id']) ?></th>
            <th><?php echo $quality_list['quantity'] ?></th>
            <th><?php echo $quality_list['check_quantity'] ?></th>
            <th><?php echo $quality_list['qualified_quantity'] ?></th>
            <th><?php echo $quality_list['unquality_count'] ?></th>
            <th><?php
                if($quality_list['check_quantity'] > 0)
                {
                    echo round($quality_list['qualified_quantity']/$quality_list['check_quantity']*100,2)."%" ;
                }
                else
                {
                    echo "---";
                }; ?></th>

        </tr>
    <?php }?>
<?php }?>
</tbody>
<!--<tfoot>-->
<!--<td>合计</td>-->
<!--<td></td>-->
<!--<td></td>-->
<!--<td></td>-->
<!--<td></td>-->
<!--<td></td>-->
<!--<td></td>-->
<!--<td></td>-->
<!--<td></td>-->
<!--<td></td>-->
<!--<td></td>-->
<!--<td></td>-->
<!--<td></td>-->
<!--<td><span id="sum_check_quantity">0</span></td>-->
<!--<td><span id="sum_qualified_quantity">0</span></td>-->
<!--<td><span id="sum_unquality_count">0</span></td>-->
<!--<td><span id="qualified_rate">0</span>%</td>-->
<!--</tfoot>-->
</table>
<!-- <textarea id="test_json" ></textarea> -->


<div>
    <?php if (isset($pagination)) {
        echo $pagination;
    } ?>
</div>

<script type="text/javascript">
    $(document).ready(function () {
        laydate.render({
            elem: '#invoice_date_from',
            type: 'datetime'
        });
        laydate.render({
            elem: '#invoice_date_to',
            type: 'datetime'
        });
        $("#supplier_name").autocomplete({
            source: '<?php echo URL::base() ?>purchase/get_supplier',
            focus: function( event, ui ) {
            $( "#supplier_name" ).val( ui.item.name );
            $( "#supplier_id" ).val( ui.item.id );
            return false;
            },
        });

    });

    $(window).scroll(function () {
        //此方法是在滚动条滚动时发生的函数
        // 当滚动到最底部以上100像素时，加载新内容
        var $doc_height, $s_top, $now_height;
        $doc_height = $(document).height();        //这里是document的整个高度
        $s_top = $(this).scrollTop();            //当前滚动条离最顶上多少高度
        $now_height = $(this).height();            //这里的this 也是就是window对象
        // if(($doc_height - $s_top - $now_height) < 100) //get_add();
    });

    uri = "";

    function get_uri() {
        var invoice_date_from = $("#invoice_date_from").val();
        var invoice_date_to = $("#invoice_date_to").val();
        var sku = $.trim($("#sku").val());
        var supplier_name = $.trim($("#supplier_name").val());
        var supplier_id = $.trim($("#supplier_id").val());
        var enterprise_dominant = $.trim($("#enterprise_dominant").val());
        //var batch_code = $.trim($("#batch_code").val());

        uri += "?invoice_date_from=" + invoice_date_from;
        uri += "&invoice_date_to=" + invoice_date_to;
        uri += "&sku=" + sku;
        uri += "&supplier_name=" + supplier_name;
        uri += "&supplier_id=" + supplier_id;
        uri += "&enterprise_dominant=" + enterprise_dominant;
        //uri += "&batch_code=" + batch_code;
    }
    //查询条件
    function get_search_data() {

        var invoice_date_from = $("#invoice_date_from").val();
        var invoice_date_to = $("#invoice_date_to").val();
        var sku = $.trim($("#sku").val());
        var supplier_name = $("#supplier_name").val();
        var supplier_id = $("#supplier_id").val();
        var enterprise_dominant = $("#enterprise_dominant").val();
        //var batch_code = $("#batch_code").val();

        return '\"invoice_date_from\": \"' + invoice_date_from + '\",' +
            '\"invoice_date_to\": \"' + invoice_date_to + '\",' +
            '\"sku\": \"' + sku + '\",' +
            //'\"batch_code\": \"' + batch_code + '\",' +
            '\"supplier_name\": \"' + supplier_name + '\" ,'+
            '\"enterprise_dominant\": \"' + enterprise_dominant + '\" ,'+
            '\"supplier_id\": \"' + supplier_id + '\" ';
    }

    //获取分页信息
    function get_pagination() {
        var per_page = $("#per_page").val();//每一页多少个
        var current_page = $("#current_page").val();//当前是那一页

        return '\"limit\": \"' + per_page + '\",' +
            '\"offset\": \"' + current_page + '\"';

    }

    //显示查看更多
    function show_more_btn() {
        var sum_ids = parseInt($("#sum_ids").text());
        var per = parseInt($("#per_page").val());
        var current_page = parseInt($("#current_page").val());
        var left = sum_ids - current_page;

        $("#left_ids").text(left);
        if (left > 0) {
            $("#add_more").removeClass('display_div');
        }
        else {
            $("#add_more").addClass('display_div');
        }
    }

    //下载按钮
    function show_search_load() {
        var sum_ids = parseInt($("#sum_ids").text());
        if (sum_ids <= 0) {
            $("#search_load").addClass('display_div');
        }
        else {
            $("#search_load").removeClass('display_div');
        }
    }

    //追加表格
    function append_table(data) {
        $("#div_content").append(data);
    }

    //清空表格
    function empty_table() {
        uri = "";
        $("#current_page").val(0);
        $("#sum_ids").text(0);
        $("#div_content").empty();
    }

    //设置当前页的下一页
    function set_pagination_next() {
        $("#current_page").val(parseInt($("#current_page").val()) + parseInt($("#per_page").val()));
    }

    //获取查询结果
    function get_ajax(details) {

        var re = {};

        $.ajax({
            type: 'post',
            url: '<?php echo url::base(TRUE);?>qualityfrom/detail/select',
            dataType: 'html',
            data: details,
            async: false,
            success: function (data) {
                re = data;
            },
            error: function () {

            }
        });

        return re;
    }


    //获取合计信息
    function get_ajax_sum(details) {

        var re = {};

        $.ajax({
            type: 'post',
            url: '<?php echo url::base(TRUE);?>qualityfrom/detail/select_sum',
            dataType: 'json',
            data: details,
            async: false,
            success: function (data) {
                re = data;
            },
            error: function () {

            }
        });

        return re;
    }

    //
    $("#add_more").click(function () {
        var details = get_search_data();
        var page = get_pagination();


        var re = get_ajax($.parseJSON('{' + details + ',' + page + '}'));

        if (re) {
            append_table(re);
            set_pagination_next();
            show_more_btn();

        }

    });

    $("#search_btn").click(function () {

        empty_table();
        var details = get_search_data();
        var page = get_pagination();

        //查询当前结果
        var re = get_ajax($.parseJSON('{' + details + ',' + page + '}'));

        if (re) {
            append_table(re);
        }

        //查询SUM结果
        var sum = get_ajax_sum($.parseJSON('{' + details + '}'));

        if (sum) {
            $("#sum_ids").text(sum.sum_ids);
            $("#sum_check_quantity").text(sum.sum_check_quantity);
            $("#sum_qualified_quantity").text(sum.sum_qualified_quantity);
            $("#sum_unquality_count").text(sum.sum_unquality_count);
            $("#qualified_rate").text(sum.qualified_rate);
        }

        if (re && sum) {
            get_uri();
            set_pagination_next();
        }
        show_more_btn();
        show_search_load();

    });

    //下载
    $("#search_load").click(function () {
        if (uri) {
            window.location.href = "<?php echo url::base(TRUE)?>qualityfrom/detail/load" + uri;
        }

    });

    // $("#batch_code").autocomplete({
    //     autoFocus: true,
    //     minLength: 0,
    //     autoFill: true,
    //     source: '<?php echo URL::base(TRUE)?>quality/getBatchCode',
    //     select: function (event, ui) {
    //         /**
    //          * 从建议列表中选中之后触发的事件
    //          */
    //         $("#batch_code").val(ui.item.name);
    //         return false;
    //     }
    // });
    function resetDate() {
        $('.input-medium').val('');
        $('.input2').val('');
        $('.input-mini').val('');
        $('#export_tax_rebate').val('-1');
    }
</script>
